<?php
class ItemsController extends AppController {

	var $name = 'Items';
	var $components = array('Auth');
	
	//Authorization things.
	function beforeFilter() {
        $this->Auth->allow('index','view','search');
	}


	function index() {
		
		$items = $this->paginate();
		if (isset($this->params['requested'])) {
			return $items;        
		} else {            
			$this->set('items', $items);
		}
		$this->set('no_items', $this->Item->find('count'));
	}
	
	function firstPageRequest() {
		
	}
	
	function add() {
		
		$this->set('categories', $this->Item->Category->find('list'));
		
		if (!empty($this->data)) {
			if ($this->Item->save($this->data)) {
				$this->Session->setFlash('A new item that has to resign has been added.');
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
	function view($id = null) {
		$this->Item->id = $id;
		$this->set('item', $this->Item->read());
	}
	
	//Search function
	function search() { 
        $this->set('results',$this->Item->search($this->data['Item']['q'])); 
    } 
	
}
?>